import java.util.Scanner;

public class Main {
    private static Converter num1;
    private static Converter num2;

    static void initial() {
        boolean flag = true;
        while (flag) {
            System.out.println("Choose system: ");
            System.out.println("1 - BIN");
            System.out.println("2 - OCT");
            System.out.println("3 - DEC");
            System.out.println("4 - HEX");
            System.out.println("5 - Back");
            Scanner sc = new Scanner(System.in);
            try {
                switch (sc.nextLine()) {
                    case "1": {
                        System.out.println("First number: ");
                        num1 = new Bin(sc.nextLine());
                        System.out.println("Second number: ");
                        num2 = new Bin(sc.nextLine());
                        System.out.println("Operation: ");
                        operation(num1, num2);
                        break;
                    }
                    case "2": {
                        System.out.println("First number: ");
                        num1 = new Oct(sc.nextLine());
                        System.out.println("Second number: ");
                        num2 = new Oct(sc.nextLine());
                        System.out.println("Operation: ");
                        operation(num1, num2);
                        break;
                    }
                    case "3": {
                        System.out.println("First number: ");
                        num1 = new Dec(sc.nextLine());
                        System.out.println("Second number:");
                        num2 = new Dec(sc.nextLine());
                        System.out.println("Operation: ");
                        operation(num1, num2);
                        break;
                    }
                    case "4": {
                        System.out.println("First number: ");
                        num1 = new Hex(sc.nextLine());
                        System.out.println("Second number: ");
                        num2 = new Hex(sc.nextLine());
                        System.out.println("Operation: ");
                        operation(num1, num2);
                        break;
                    }
                    case "5": {
                        flag = false;
                        break;
                    }
                    default: {
                        System.out.println("Unknown operation\n"); break;
                    }
                }
            }
            catch (Exception e) {
                System.out.println(e.getMessage() + " Try again\n");
            }
        }
    }

    static void operation(Converter numsys1, Converter numsys2) {
        try {
            System.out.println("+");
            System.out.println("-");
            System.out.println("*");
            System.out.println("/");
            System.out.println("5. Back");
            Scanner sc = new Scanner(System.in);
            Calculator calc = new Calculator();
            Converter result;
            String operation = sc.nextLine();
            switch (operation) {
                case "+": {
                    result = calc.summ(numsys1, numsys2);
                    System.out.println(result);
                    System.out.println(result.print());
                    break;
                }
                case "-": {
                    result = calc.substr(numsys1, numsys2);
                    System.out.println(result);
                    System.out.println(result.print());
                    break;
                }
                case "*": {
                    result = calc.multi(numsys1, numsys2);
                    System.out.println(result);
                    System.out.println(result.print());
                    break;
                }
                case "/": {
                    result = calc.div(numsys1, numsys2);
                    System.out.println(result);
                    System.out.println(result.print());
                    break;
                }
                case "5": break;
                default: {
                    System.out.println("Unknown operation\n");
                    break;
                }
            }
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public static void main(String[] args) {
        initial();
    }
}